<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>销售人员后台-增改商品</title>
  <link rel="shortcut icon" href="/public/img/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" href="/node_modules/layui/dist/css/layui.css">
</head>

<body>
  <div class="layui-layout layui-layout-admin">
    <div class="layui-header">
      <div class="layui-logo" style="width: 250px;">销售人员 {{ user.username }} 的商品管理页面</div>
      <ul class="layui-nav layui-layout-right">
        <li class="layui-nav-item">
          <a href="javascript:;">
            <img src="/public/img/head.jpg" class="layui-nav-img">
            {{ user.username }}
          </a>
        </li>
        <li class="layui-nav-item"><a href="/logout">注销</a></li>
      </ul>
    </div>

    <div class="layui-side layui-bg-black">
      <div class="layui-side-scroll">
        <ul class="layui-nav layui-nav-tree" lay-filter="test">
          <li class="layui-nav-item"><a href="/salesman">商品列表&删除</a></li>
          <li class="layui-nav-item"><a href="/addProduct">添加&修改商品</a></li>
          <li class="layui-nav-item"><a href="/records">用户购买记录</a></li>
        </ul>
      </div>
    </div>

    <div class="layui-body">
      <div style="padding: 15px;">
        <form id="add-form" class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <label class="layui-form-label">商品名称</label>
            <div class="layui-input-block">
              <input type="text" name="productName" required  lay-verify="required" placeholder="请输入商品名称" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
              <input type="text" name="price" required  lay-verify="required" placeholder="请输入商品价格" autocomplete="off" class="layui-input">
            </div>
          </div>
          <input type="text" name="hand" value="add" style="display: none;">
          <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 10px;">
              <button class="layui-btn">添加商品</button>
              <button type="reset" class="layui-btn layui-btn-primary" id="add-reset">重置</button>
            </div>
          </div>
        </form>
        <br>
        <br>
        <form id="revise-form" class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <label class="layui-form-label">商品id</label>
            <div class="layui-input-block">
              <input type="text" name="productID" required  lay-verify="required" placeholder="请输入要修改的商品id" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">商品价格</label>
            <div class="layui-input-block">
              <input type="text" name="price" required  lay-verify="required" placeholder="请输入商品价格" autocomplete="off" class="layui-input">
            </div>
          </div>
          <input type="text" name="hand" value="revise" style="display: none;">
          <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 10px;">
              <button class="layui-btn">修改商品信息</button>
              <button type="reset" class="layui-btn layui-btn-primary" id="revise-reset">重置</button>
            </div>
          </div>
        </form>
      </div>
    </div>

  </div>

  <script src="/node_modules/layui/dist/layui.js"></script>
  <script>
    //JavaScript代码区域
    layui.use('element', function () {
      var element = layui.element;

    });
  </script>
  <script src="/node_modules/jquery/dist/jquery.js"></script>
  <script>
    //异步提交表单
    $('#add-form').on('submit', function (e) {
      e.preventDefault()
      var formData = $(this).serialize()
      $.ajax({
          url: '/addProduct',
          type: 'post',
          data: formData,
          dataType: 'json',
          success: function (data) {
            var err_code = data.err_code
            if (err_code === 1) {
              window.alert('成功添加商品！')
              $('#add-reset').click()
            }
          }
        })
    })
    $('#revise-form').on('submit', function (e) {
      e.preventDefault()
      var formData = $(this).serialize()
      $.ajax({
          url: '/addProduct',
          type: 'post',
          data: formData,
          dataType: 'json',
          success: function (data) {
            var err_code = data.err_code
            if (err_code === 1) {
              window.alert('成功修改商品信息！')
              $('#revise-reset').click()
            }else if(err_code === 0){
              window.alert('该商品不存在，请检查商品id')
            }
          }
        })
    })
  </script>
</body>

</html>